package cn.kgc.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.base.entity.SysDict;
import cn.kgc.base.dto.SysDictAddDTO;
import cn.kgc.base.dto.SysDictUpdateDTO;
import cn.kgc.base.service.SysDictService;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.base.entity.SysDict;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * (SysDict)表控制层
 *
 * @author 石浩杰
 * @since 2022-11-30 14:50:38
 */
@RestController
@RequestMapping("sysDict")
@Api(tags = "字典管理")
@Validated
@Slf4j
@ConditionalOnClass(ResponseBodyAdvice.class)
public class SysDictController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictService sysDictService;

    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "dictName", value = "字典名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictCode", value = "字典编码", paramType = "query", dataType = "String")
    })
    @CrossOrigin
    public IPage<SysDict> selectAll(@RequestParam(defaultValue = "1") int current,
                                    @RequestParam(defaultValue = "5") int pageSize,
                                    @RequestParam(defaultValue = "") String dictName,
                                    @RequestParam(defaultValue = "") String dictCode) {
        log.debug("查询数据字典,current:{},pageSize:{},dictName:{},dictCode:{}", current, pageSize, dictName, dictCode);
        LambdaQueryWrapper<SysDict> queryWrapper = new QueryWrapper<SysDict>().lambda();
        queryWrapper.like(StringUtils.isNotBlank(dictName),SysDict::getDictName, dictName);
        queryWrapper.like(StringUtils.isNotBlank(dictCode),SysDict::getDictCode, dictCode);
        IPage<SysDict> page = new Page(current, pageSize);
        return sysDictService.page(page, queryWrapper);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public SysDict selectOne(@PathVariable Serializable id) {
        return this.sysDictService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param sysDictDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody SysDictAddDTO sysDictDTO) {
        log.debug("新增数据字典,sysDictDTO:{}", sysDictDTO);
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictDTO, sysDict);
        String createBy="scott"; //todo 创建人后面从网关获得
        sysDict.setCreateBy(createBy);
        return this.sysDictService.save(sysDict);
    }

    /**
     * 修改数据
     *
     * @param sysDictDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody SysDictUpdateDTO sysDictDTO) {
        SysDict sysDict = this.sysDictService.getById(sysDictDTO.getId());
        BeanUtils.copyProperties(sysDictDTO, sysDict);
        String updateBy="scott"; //todo 更新人后面从网关获得
        sysDict.setUpdateBy(updateBy);
        return this.sysDictService.updateById(sysDict);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "String")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.sysDictService.removeById(id);
    }
}
